from rest_framework import serializers
from question.models import Label, Question, Reply
from headlines.models import Comment
from user.models import User


class LabelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = [
            'id',
            'label_name'
        ]


class QuestionSimpleSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    user_id = serializers.IntegerField()

    class Meta:
        model = Question
        fields = "__all__"


class QuestionSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    user_id = serializers.IntegerField()
    labels = serializers.StringRelatedField(many=True)

    class Meta:
        model = Question
        fields = "__all__"


class UserSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'avatar']


class UserSerializer(serializers.ModelSerializer):
    labels = LabelSerializer(many=True)

    class Meta:
        model = User
        fields = ['id', 'username']


class SubReplySerializer(serializers.ModelSerializer):
    user = UserSimpleSerializer(read_only=True)

    class Meta:
        model = Reply
        fields = [
            'id',
            'content',
            'createtime',
            'useful_count',
            'unuseful_count',
            'user'
        ]


class ReplySerializer(serializers.ModelSerializer):
    subs = SubReplySerializer(read_only=True, many=True)
    user = UserSerializer()

    class Meta:
        model = Reply
        fields = [
            'id',
            'content',
            'createtime',
            'useful_count',
            'problem',
            'unuseful_count',
            'subs',
            'user',
            'parent'
        ]


class CommentSerializer(serializers.ModelSerializer):
    subs = SubReplySerializer(read_only=True, many=True)
    user = UserSerializer()

    class Meta:
        model = Comment
        fields = [
            'id',
            'content',
            'createtime',
            'useful_count',
            'problem',
            'unuseful_count',
            'subs',
            'user',
            'parent'
        ]


class QuestionInfoSerializer(serializers.ModelSerializer):
    # answer_question = ReplySerializer(read_only=True, many=True)
    # comment_question = CommentSerializer(read_only=True, many=True)
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(many=True)

    class Meta:
        model = Question
        fields = [
            'id',
            'createtime',
            'labels',
            'reply',
            'replyname',
            'replytime',
            'title',
            'unuseful_count',
            'useful_count',
            'user',
            'visits',
            'content',
        ]

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['comment_question'] = []
        data['answer_question'] = Question.objects.get(pk=instance.id).replies.all()
        return data


class QuestionMostSimpleSerializer(serializers.ModelSerializer):
    # user = serializers.StringRelatedField(read_only=True)
    # user_id = serializers.IntegerField()

    class Meta:
        model = Question
        fields = ['id']
